<template>
	<view class="box">
		<view class="hezi">
			<view class="toux">
				<span>头像</span>
				<view @click="updateImg">
					<image v-if="userInfo.headImg" :src="userInfo.headImg" mode=""></image>
					<image v-else src="../../static/1.jpg" mode=""></image>
					<uni-icons type="right" size="20"></uni-icons>
				</view>

			</view>
			<view class="toux" style="border-bottom: none;">
				<span>昵称</span>
				<view @click="setnickname">
					<span>{{userInfo.nickName}}</span>
					<uni-icons type="right" size="20"></uni-icons>
				</view>
			</view>
		</view>
		<!-- 输入框示例 -->
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="修改昵称" v-model='nickname' placeholder="请输入昵称"
				@confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getUserinfo,
		setUserinfo
	} from '../../common/http.api.js'
	export default {
		data() {
			return {
				userInfo: {},
				nickname:''
			}
		},
		onLoad() {
			getUserinfo().then(res => {
				this.userInfo = res.data
			})
		},
		methods: {
			dialogInputConfirm(){
				this.userInfo.nickName = this.nickname
				if(this.userInfo.nickName == ''){
					uni.showToast({
						title: '昵称不能为空',
						icon: "none"
					})
				}else{
					let params = {
						id: this.userInfo.id,
						nickName: this.userInfo.nickName
					}
					setUserinfo(params).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: '修改成功',
								duration: 2000,
								icon: "success"
							})
							getUserinfo().then(res => {
								this.userInfo = res.data
							})
						}
					})
				}
				
			},
			setnickname(){
				this.$refs.inputDialog.open()
			},
			updateImg() {
				uni.chooseImage({
					sourceType: ['album'], //从相册选择
					success: chooseImageRes => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						const uploadTask = uni.uploadFile({
							url: 'http://brick.print.heiwangke.net/brick-api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								Authorization: uni.getStorageSync('token'),
							},
							success: (res) => {
								let img = JSON.parse(res.data).url
								let params = {
									id: this.userInfo.id,
									headImg: img,
									nickName: this.userInfo.nickName
								}
								setUserinfo(params).then(res => {
									if (res.code == 200) {
										uni.hideLoading();
										uni.showToast({
											title: '图片上传成功',
											duration: 2000,
											icon: "success"
										})
										getUserinfo().then(res => {
											this.userInfo = res.data
										})
									}
								})
							}
						});
						uploadTask.onProgressUpdate((res) => {
							if (res.progress < 100) {
								uni.showLoading({
									title: '头像上传中'
								});
							}
						});
					},
					fail: err => {
						uni.showToast({
							title: '图片上传失败',
							duration: 2000,
							mask: false,
							icon: "none"
						})
					}
				});
			},

		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F6F6F6;
	}

	.box {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	button {
		width: 695rpx;
		height: 80rpx;
		background: #065CF7;
		border-radius: 40rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 28rpx;
		color: #FFFFFF;
		margin-top: 60rpx;
		line-height: 80rpx;
	}

	.toux {
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px #EEEEEE solid;
		height: 120rpx;

		span {
			font-family: PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;
		}

		view {
			display: flex;
			align-items: center;
		}

		image {
			width: 98.1rpx;
			height: 98.1rpx;
			border-radius: 50%;
		}
	}

	.hezi {
		width: 660rpx;
		background: #FFFFFF;
		border-radius: 14rpx;
		margin-top: 20rpx;
		padding: 20rpx;
	}
</style>